package com.caiyouxi.user.service;

import com.caiyouxi.common.bean.ResData;
import com.caiyouxi.common.exception.ErrorCode;
import com.caiyouxi.common.service.BaseService;
import com.caiyouxi.common.util.Log;
import com.caiyouxi.user.mapper.UserProfileMapper;
import com.caiyouxi.user.model.UserAccountBean;
import com.caiyouxi.user.model.UserProfile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserProfileService extends BaseService{

    @Autowired
    UserProfileMapper userProfileMapper;

    public ResData get_info(UserProfile bean) {

        return success(userProfileMapper.get_by_id(bean.getId()));
    }
    public UserProfile get_user_by_id(int user_id) {

        String redis_key = "UserProfile_"+user_id;
        Log.d("search redis..."+redis_key);
        UserProfile db_bean = (UserProfile) redisService.get(redis_key);
        if (null == db_bean){
            Log.d("search db...");
            db_bean  = userProfileMapper.get_by_id(user_id);
            redisService.set(redis_key,db_bean);
        }
        return  db_bean;
    }

    public ResData update(UserProfile bean) {
        int result = userProfileMapper.update(bean);
        if(result == 1){
            return success(null);
        }else {
            return failed(ErrorCode.SYS_BUSSINESS_ERROR.getCode(),ErrorCode.SYS_BUSSINESS_ERROR.getMsg(),null);
        }
    }

    public UserAccountBean get_user_account_by_source(int user_id,String source) {

        UserAccountBean search = new UserAccountBean();
        search.setUser_id(user_id);
        search.setSource(source);
        return userProfileMapper.get_user_account_by_source(search);

    }
}
